﻿using MangoMes.Commont.Domain.ProductionWorkOrders;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.DTOs.CommontFiles;
using MangoMes.Write.API.Applications.ProductionWorkOrders;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Globalization;

namespace MangoMes.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WorkOrderController : ControllerBase
    {
        /// <summary>
        /// 生产构造函数
        /// </summary>
        private readonly IMediator _mediator;

        public WorkOrderController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 生产工单添加、编辑
        /// </summary>
        /// <param name="info">生产工单</param>
        /// <returns>返回受影响结果</returns>
        [HttpPost]
        public async Task<Resqults> ProductionCreateOrUpdate(ProductionOrdersCommand info)
        {
            try
            {
                var res = await _mediator.Send(info);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 生产工单删除
        /// </summary>
        /// <param name="proNo">工单编码</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultsDTO> ProductionCreateOrDelete(string proNo)
        {
            try
            {
                var res = await _mediator.Send(new ProductionOrdersDeleteCommand() { ProNo = proNo });
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 生产工单导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Resqults> ImportProduction(IFormFile file)
        {
            Resqults res = new Resqults();
            var exName = Path.GetExtension(file.FileName);
            if (exName == ".xls" || exName == ".xlsx")
            {

            }
            else
            {
                res.Messges = "传输的文件格式有误";
                return res;
            }
            using (var ms = new MemoryStream())
            {

                file.CopyTo(ms);
                ms.Position = 0;
                IWorkbook wb = null;
                if (exName == ".xls")//创建87-2003
                {
                    wb = new HSSFWorkbook(ms);
                }
                else
                {
                    wb = new XSSFWorkbook(ms);
                }
                ISheet sheet = wb.GetSheetAt(0);
                int rowNum = sheet.LastRowNum;
                int cellNum = sheet.GetRow(0).LastCellNum;
                ProductionOrdersCommand order = new ProductionOrdersCommand();
                order.ProductionWorkOrdersID = 0;
                for (int i = 1; i <= rowNum; i++)
                {
                    order.ProductionWorkOrdersNo = sheet.GetRow(i).GetCell(0).ToString();
                    order.ProductionWorkOrdersName = sheet.GetRow(i).GetCell(1).ToString();
                    order.Source = sheet.GetRow(i).GetCell(2).ToString();
                    order.OrderNumber = sheet.GetRow(i).GetCell(3).ToString();
                    order.TicketsType = sheet.GetRow(i).GetCell(4).ToString();
                    order.ProductsNo = sheet.GetRow(i).GetCell(5).ToString();
                    order.TicketsNum = Convert.ToInt32(sheet.GetRow(i).GetCell(9).ToString());
                    order.CustomerNo = sheet.GetRow(i).GetCell(11).ToString();
                    order.Remark = sheet.GetRow(i).GetCell(14).ToString();
                    order.State = 0;
                    var a = sheet.GetRow(i).GetCell(10).ToString();
                    string[] formats = { "dd-M月-yyyy", "yyyy-MM-dd" };
                    DateTime thisDate;
                    foreach (var format in formats)
                    {
                        if (DateTime.TryParseExact(a, new[] { format }, CultureInfo.InvariantCulture, DateTimeStyles.None, out thisDate))
                        {
                            order.DemandDate = Convert.ToDateTime(thisDate.ToString("yyyy-MM-dd"));
                            break;
                        }
                    }

                    order.BatchNo = sheet.GetRow(i).GetCell(11).ToString();
                    order.QrCore = "";
                    order.Pid = 0;
                    res = await ProductionCreateOrUpdate(order);
                }
                return res;
            }
        }

    }
}
